<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@include file="_include.jsp" %>

<html>
<%--路径 /forum/section/${sectionId}/article--%>
<head>
    <script src="${pageContext.request.contextPath}/resources/js/jquery-2.1.1.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>发贴</title>
    <style type="text/css">
        #content {
            width: 300px;
            height: 200px;
        }
    </style>
</head>
<body>
<h1>发贴</h1>

<p id="showInfo"></p>

title:<br/>
<input type="text" id="title"><br/>

content:<br/>
<input id="content" type="text"><br/>
<button id="submit">发表</button>

<script type="text/javascript">
    var showInfo = $('#showInfo')
    $('#submit').click(function () {
        console.log('提交文章'); //TODO
        var title = $('#title').val();
        if (!title) {
            $('#showInfo').val("标题不能为空");
            return;
        }
        var content = $('#content').val();
        if (!content) {
            $('#showInfo').val("内容不能为空");
            return;
        }

        var upData = {
            title: title,
            content: content
        }
        $.ajax({
            type: 'POST',
            url: '/forum/section/${sectionId}/article',
            data: JSON.stringify(upData),
            contentType: "application/json",
            dataType: "json",
            success: function (data) {
                console.log(data);
                if (typeof data === 'string') {
                    data = JSON.parse(data);
                }

                if (data.isSucc === "true") {
                    window.location = "/forum/section/${sectionId}/page/0"; // TODO
                } else {
                    $("#showInfo").text(data.errMsg);
                }
            }, error: function (errMsg) {
                console.log(errMsg);
                $("#showInfo").text(errMsg);
            }
        });
    });
</script>
</body>
</html>
